home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 2191 < prev    next >
Encoding:
Text File  |  1996-08-06  |  1.7 KB  |  70 lines

  1. Path: classic.iinet.com.au!news
  2. From: ng@mitswa.com.au (John A Ng)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Dumb Question? about ptr
  5. Date: Tue, 16 Jan 1996 09:09:37 GMT
  6. Organization: MITS (WA)
  7. Message-ID: <4dfq0e$btq@classic.iinet.com.au>
  8. References: <00001a81+00008b8f@msn.com>
  9. NNTP-Posting-Host: grunge205.nv.iinet.net.au
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. Namic@msn.com (The ugly pink sweater book The Orange book is) wrote:
  13.  
  14. >Dont laugh im only been programming in this for a few hours but why is
  15. >this giving me the following errors in Line 11 (I copied straight out 
  16. >o a how to program in C++ book. Yes the compilers match i have 
  17. >Borland C++ 4.02 
  18.  
  19. >Error NONAME00.CPP 11: Type name expected
  20. >Error NONAME00.CPP 11: Variable 'foo' is initialized more than once
  21. >Error NONAME00.CPP 11: Improper use of typedef 'MyStruct'
  22. >                    
  23.  
  24. >#include <iostream.h>                    
  25.  
  26. >class MyStruct {
  27. >    public:
  28. >    int data;
  29. >    int value;
  30. >};
  31.  
  32. >MyStruct *foo;
  33. >MyStruct Record1;
  34. >foo = &MyStruct;        // Line 11
  35.  
  36. >void main() {
  37. >    foo->data=5;
  38. >    cout << foo->data;
  39. >};
  40.  
  41.  
  42. I would like to know the name of the book so I could recommend to people
  43. not to read it.
  44.  
  45. Line 11 is an executable statement and it should be written inside a
  46. function (ie inside main()).
  47.  
  48. However, this will still not compile since MyStruct is only a definition
  49. and no object exists.  What should be done is:
  50.  
  51.  
  52.  
  53. you are trying to write an executable statement outside of any function.
  54. You should instead say:
  55.  
  56.     MyStruct *foo = &MyStruct;
  57.  
  58. However, this will still not compile because MyStruct is only a
  59. declaration.  There is yet no object.  You will need to create a real
  60. object by instantiating the class declaration as:
  61.  
  62.     MyStruct *foo = new MyStruct;
  63.  
  64. Regards,
  65.  
  66. John Ng
  67. ng@mitswa.com.au
  68. Western Australia
  69.  
  70.